package org.qq.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.qq.clientthread.ClientModifyThread;
import org.qq.util.Util;


/**
 * 
 * project_name:QQ
 * package_name:org.qq.register
 * file_name:PrivateMerely.java
 * @author cui
 * create_date:2015年8月27日 下午5:40:20
 * modified_date: 2015年8月31日 下午4:00:20
 * description:用户修改界面（主界面下的子界面）
 *
 */
public class Modify extends JFrame{
	private ImageIcon tu;											//图片
	private JButton modifyButton,groupButton,privateChatButton;		//菜单栏Button按钮
	private JMenuBar menuBar;										//菜单栏
	private JLabel nameLabel;										//用户名标签
	private JLabel nickNameLabel;									//昵称标签
	private JLabel passwordLabel;									//密码标签
	private JTextField nameField;									//用户名文本框
	private JTextField nickNameField;								//昵称文本框
	private JTextField passwordField;								//密码文本框
	private JTextField passwordField1;								//密码确认文本框
	private JButton ensureButton;									//确认按钮
	private JButton resetButton;									//重置按钮
	private JRadioButton manButton;									//男按钮
	private JRadioButton womenButton;								//女按钮
	private JLabel passwordLabel1;									//密码确认标签
	private JLabel welcomLabel;										//欢迎面板
	private ButtonGroup buttonGroup;
	private Socket s;												//套接字
	private JLabel promptLabel,promptLabel1;						//提示标签
	private String name;											//用户帐号信息
	
	
	
	public Modify() throws HeadlessException {						//无參构造方法
		super();
	}
	
	public Socket getS() {
		return s;
	}

	public void setS(Socket s) {
		this.s = s;
	}

	public Modify(Socket s,String name){
		this.s = s;
		this.name = name;
		setBackground(Color.GRAY);
		getContentPane().setBackground(Color.LIGHT_GRAY);
		this.setBounds(0, 0, 800, 600);
		this.setTitle("用户修改界面");
		Util.screen(this);																//调用Util包里面居中方法
		//this.setDefaultCloseOperation(3);
		this.setResizable(false); 														// 不可改变大小
		getContentPane().setLayout(null);
		
		this.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				Modify.this.setVisible(false);
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		menuBar = new JMenuBar();
		menuBar.setBackground(Color.GRAY);
		setJMenuBar(menuBar);
		
		modifyButton = new JButton("用户修改");
		modifyButton.setForeground(Color.BLACK);
		modifyButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		modifyButton.setBackground(Color.PINK);
		menuBar.add(modifyButton);
		modifyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(null, "已在用户设置界面界面");
			}
		});
		
		groupButton = new JButton("群聊");
		groupButton.setForeground(new Color(0, 0, 0));
		groupButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		groupButton.setBackground(new Color(192, 192, 192));
		menuBar.add(groupButton);
		
		privateChatButton = new JButton("私聊");
		privateChatButton.setForeground(new Color(0, 0, 0));
		privateChatButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		privateChatButton.setBackground(new Color(192, 192, 192));
		menuBar.add(privateChatButton);
		
		tu = new ImageIcon(PrivateChatRoom.class.getResource("/image/WSSFilesToolHomePageBackground.jpg"));
		welcomLabel = new JLabel(tu);
		welcomLabel.setBounds(0, 0, 794, 146);
		getContentPane().add(welcomLabel);
		
		nameLabel = new JLabel("用户名");
		nameLabel.setFont(new Font("幼圆", Font.PLAIN, 17));
		nameLabel.setBackground(Color.DARK_GRAY);
		nameLabel.setBounds(161, 147, 92, 46);
		getContentPane().add(nameLabel);
		
		nickNameLabel = new JLabel("新昵称");
		nickNameLabel.setFont(new Font("幼圆", Font.PLAIN, 17));
		nickNameLabel.setBounds(161, 201, 92, 46);
		getContentPane().add(nickNameLabel);
		
		passwordLabel = new JLabel("新密码");
		passwordLabel.setFont(new Font("幼圆", Font.PLAIN, 17));
		passwordLabel.setBounds(161, 255, 92, 46);
		getContentPane().add(passwordLabel);
		
		nameField = new JTextField(name);
		nameField.setBounds(277, 156, 216, 32);
		getContentPane().add(nameField);
		nameField.setEditable(false);
		nameField.setColumns(10);
		
		nickNameField = new JTextField();
		nickNameField.setColumns(10);
		nickNameField.setBounds(277, 210, 216, 32);
		getContentPane().add(nickNameField);
		
		passwordField = new JTextField();
		passwordField.setColumns(10);
		passwordField.setBounds(277, 255, 216, 32);
		getContentPane().add(passwordField);
		
		passwordLabel1 = new JLabel("新密码确认");
		passwordLabel1.setFont(new Font("幼圆", Font.PLAIN, 17));
		passwordLabel1.setBounds(161, 299, 92, 46);
		getContentPane().add(passwordLabel1);
		
		passwordField1 = new JTextField();
		passwordField1.setColumns(10);
		passwordField1.setBounds(277, 299, 216, 32);
		getContentPane().add(passwordField1);
		
		buttonGroup = new ButtonGroup();
		manButton = new JRadioButton("男",true);
		manButton.setForeground(new Color(0, 0, 0));
		manButton.setBackground(new Color(128, 128, 128));
		manButton.setBounds(281, 351, 61, 32);
		getContentPane().add(manButton);
		buttonGroup.add(manButton);
		
		womenButton = new JRadioButton("女");
		womenButton.setBackground(new Color(238, 232, 170));
		womenButton.setBounds(426, 351, 61, 32);
		getContentPane().add(womenButton);
		buttonGroup.add(womenButton);
		
		ensureButton = new JButton("确认修改");
		ensureButton.setBackground(Color.ORANGE);
		ensureButton.setFont(new Font("幼圆", Font.PLAIN, 17));
		ensureButton.setForeground(Color.DARK_GRAY);
		ensureButton.setBounds(443, 412, 135, 58);
		getContentPane().add(ensureButton);
		ensureButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(!Util.isDigit(nameField.getText())||nameField.getText().equals("")){
					JOptionPane.showMessageDialog(Modify.this, "帐号不能为8位数字以外的其它字符!","错误提示",JOptionPane.ERROR_MESSAGE);
				}else if(!passwordField.getText().equals(passwordField1.getText())||passwordField1.getText().equals("")){
					JOptionPane.showMessageDialog(Modify.this, "两次密码不相同,请重新输入密码!","错误提示",JOptionPane.ERROR_MESSAGE);
				}else if(!Util.passwordIsLegal(passwordField1.getText())){
					JOptionPane.showMessageDialog(Modify.this,"输入密码包含非法字符,请重新输入!","错误提示",JOptionPane.ERROR_MESSAGE);
				}else if(nickNameField.getText().equals("")){
					JOptionPane.showMessageDialog(Modify.this, "昵称不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);
				}else{
					Socket s = null;
					String ip = Modify.this.getS().getInetAddress().getHostAddress();
					int port = new Login().getServerPort();
					String modifystr = "帐号:" + nameField.getText()+ "----密码:" +passwordField1.getText()+ "----IP地址:" +Modify.this.getS().getInetAddress().getHostAddress()+ "----昵称:" + nickNameField.getText() + "----性别:" + Util.sexSelect(manButton, womenButton)+"----修改日期:" + 
										new SimpleDateFormat("yyyy年MM月dd日 HH.mm.ss").format(new Date())+"-----modify";
					try {
						s = new Socket(ip,port);
						ClientModifyThread cmt = new ClientModifyThread(s);
						cmt.start();//启动用户端修改信息线程
						cmt.sendMessageToServer(modifystr);//调用想服务器端发送信息方法
					} catch (UnknownHostException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		
		resetButton = new JButton("重置");
		resetButton.setForeground(Color.DARK_GRAY);
		resetButton.setFont(new Font("幼圆", Font.PLAIN, 17));
		resetButton.setBackground(Color.ORANGE);
		resetButton.setBounds(175, 412, 135, 58);
		getContentPane().add(resetButton);
		
		promptLabel = new JLabel("(注: 八位阿拉伯数字组成)");
		promptLabel.setForeground(Color.RED);
		promptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		promptLabel.setBounds(503, 156, 184, 32);
		getContentPane().add(promptLabel);
		
		promptLabel1 = new JLabel("(注: 数字,字母和\"-\"以外的字符组成)");
		promptLabel1.setForeground(Color.RED);
		promptLabel1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		promptLabel1.setBounds(503, 253, 266, 32);
		getContentPane().add(promptLabel1);
		resetButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				nameField.setText("");
				nickNameField.setText("");
				passwordField.setText("");
				passwordField1.setText("");
			}
		});
	}
}

